//
//  JXOrderListController.m
//  JiaoX
//
//  Created by yinqy on 15/1/5.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//


#import "JXOrderListController.h"
#import "SelfDataTool.h"
#import "OrderItemFrame.h"
#import "OrderItemCell.h"
#import "JXOrderDetailController.h"
#import "JXAccountCenterController.h"
#import "CartDataTool.h"
#import "CartOrderStatusController.h"


@interface JXOrderListController ()<UITableViewDelegate,UITableViewDataSource,OrderCellViewDelegate,AccountCenterDelegate,CartOrderStatusDelegate>
{
//    UITableView *self.tableView;
    NSMutableArray *_orderFrameArray;
}
@property (nonatomic,copy)NSString *orderType;//1有效，2交易完成，-1无效

@end

@implementation JXOrderListController
+(instancetype)getInstanceWithOrderType:(NSString *)type{
    JXOrderListController *ctr = [[JXOrderListController alloc]init];
    ctr.orderType = type;
    return ctr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    _orderFrameArray = [NSMutableArray array];
    [self initViews];
//    [self requestData];
}

-(void)initViews{
    [self.tableView setSeparatorColor:SEPARATORCOLOR];
    [self.tableView setTableFooterView:[[UIView alloc]init]];
}

-(void)requestData{
    [SelfDataTool getOrderListWithOrderType:self.orderType success:^(OrderList *result) {
        if ([Utils isBlankString:result.data]) {
            [self orderItemFramesWithData:result];
        }else{
            UIViewController *c = [[self.view superview] viewController];
            [DialogUtils showTipsWithView:c.navigationController.view String:result.data];
        }
        
    } failure:^(NSError *error) {
        UIViewController *c = [[self.view superview] viewController];
        [DialogUtils showTipsWithView:c.navigationController.view String:@"加载失败"];
    }];
}

-(void)orderItemFramesWithData:(OrderList *)result{
    [_orderFrameArray removeAllObjects];
    for (int i=0; i<result.orderList.count; i++) {
        OrderItemFrame *frame = [[OrderItemFrame alloc]init];
        //传递数据，计算所有子控件的frame
        [frame setOrderItem:result.orderList[i]];
        [_orderFrameArray addObject:frame];
    }
    [self.tableView reloadData];
}

#pragma mark - Table view data source & delegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _orderFrameArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    OrderItemCell *cell = [OrderItemCell cellWithTableView:tableView];
    cell.orderCellView.tag = indexPath.row;
    cell.orderCellView.delegate = self;
    [cell setOrderItemframe:_orderFrameArray[indexPath.row]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    OrderItemFrame *frame = _orderFrameArray[indexPath.row];
    return frame.selfFrame.size.height;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    OrderItemFrame *frame = _orderFrameArray[indexPath.row];
    [self toOrderDetail:frame.orderItem];
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

-(void)orderItemBtnClick:(NSInteger)index{
    OrderItemFrame *frame = _orderFrameArray[index];
    //去支付或者跳转到订单详情
    if ((frame.orderItem.payMode == PAYMODE_ALIPAY | frame.orderItem.payMode == PAYMODE_WXPAY) && frame.orderItem.orderStatus == 1){
        //去支付
        [self payWithOrderItem:frame.orderItem];
    }else{
        [self toOrderDetail:frame.orderItem];
    }
}

-(void)toOrderDetail:(OrderListItem *)item{
    //跳转到订单详情
    JXOrderDetailController *orderDetail = [[JXOrderDetailController alloc]init];
    orderDetail.orderItem = item;
    UIViewController *c = [[self.view superview] viewController];
    [c.navigationController pushViewController:orderDetail animated:YES];
}

-(void)payWithOrderItem:(OrderListItem *)item{
    NSMutableDictionary *param =[NSMutableDictionary dictionaryWithDictionary:[[[JXBaseRequest alloc]init] keyValues]];
    [param setObject:item.orderCode forKey:@"orderCode"];
    UIViewController *c = [[self.view superview] viewController];
    MBProgressHUD *loding = [DialogUtils showLoadingWithView:c.navigationController.view];
    [CartDataTool getAccountCenterDataWithParam:param success:^(AddressAndCheckProdInfo *result) {
        [loding hide:YES];
        if([Utils isBlankString:result.data]){//获取结算中心需要的数据成功
            result.currentPayMode = item.payMode;
            result.isImmediatelyPay = YES;
            JXAccountCenterController *ctrl = [JXAccountCenterController initWithProdInfo:result :ACCOUNT_CENTER_ORDER_DETAIL];
            ctrl.delegate = self;
            UIViewController *c = [[self.view superview] viewController];
            [c.navigationController pushViewController:ctrl animated:YES];
            
        }
    } failure:^(NSError *error) {
        [loding hide:YES];
    }];
}

-(void)accountCenterFinish:(int)type status:(NSString *)status{
    if (type==ACCOUNT_CENTER_ORDER_LIST) {
        CartOrderStatusController *cart = [CartOrderStatusController initWithResultCode:status type:ACCOUNT_CENTER_ORDER_LIST];
        cart.delegate = self;
        UIViewController *c = [[self.view superview] viewController];
        [c.navigationController pushViewController:cart animated:YES];
    }
}

-(void)orderStatusFinish:(int)type{
    if (type==ACCOUNT_CENTER_ORDER_LIST) {
        //刷新页面数据
        [self requestData];
    }
}

-(void)viewDidLayoutSubviews
{
    [Utils setSeparatorInsetWithTableView:self.tableView insets:UIEdgeInsetsZero];
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

@end
